import axios from "axios";
import { Toast } from 'antd-mobile'
import Cookies from "js-cookie";
let langChange = str => {
	switch (str) {
		case "cn":
			return "zh-Hans";
		case "en":
			return "en";
		case "tw":
			return "zh-Hant";
		case "kr":
			return "kr";
		case "ru":
			return "ru";
		case "th":
			return "th";
		case "vn":
			return "vi";
		default:
			return "en";
	}
};
/* ajax-get */
export const fetch = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		axios({
			method: "get",
			url: url,
			params: data,
			headers: {
				//
				"Content-Type": "application/json",
				// lang: "zh-Hans"
				lang: langChange(Cookies.get("lang"))
			}
		})
			.then(resp => {
				resolve(resp);
			})
			.catch(err => {
				if (err.status === 500 || err.status === 404) {
					Toast.show({
						content: 'error'
					})
				}
				reject(err);
			});
	});
};
/* json-post */
export const postJson = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		axios({
			method: "post",
			url: url,
			data: data,
			headers: {
				"Content-Type": "application/json",
				lang: "zh-Hans"
			}
		})
			.then(resp => {
				resolve(resp);
			})
			.catch(err => {
				if (err.status === 500 || err.status === 404) {
					Toast.show({
						content: 'error'
					})
				}
				reject(err);
			});
	});
};
/* json-put */
export const putJson = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		axios({
			method: "put",
			url: url,
			data: data,
			headers: {
				"Content-Type": "application/json",
				lang: "zh-Hans"
			}
		})
			.then(resp => {
				resolve(resp);
			})
			.catch(err => {
				if (err.status === 500 || err.status === 404) {
					Toast.show({
						content: 'error'
					})
				}
				reject(err);
			});
	});
};
